﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

namespace ProjectEuler.Problems
{
    [EulerProblem(35, 55)]
    [Description("How many circular primes are there below one million?")]
    internal sealed class Problem035 : EulerProblem
    {
        public override Object Solve()
        {
            var max = 1000000;

            var sieve = new SieveOfEratosthenes(max - 1);
            sieve.EnsureComputedUpTo(max - 1);

            return sieve.Count(prime => Rotations(prime).All(rot => sieve.IsPrime(rot)));
        }

        private static IEnumerable<Int32> Rotations(Int32 n)
        {
            var log = Math.Floor(Math.Log10(n));
            var divisor = Math.Pow(10, log);

            for (int i = 0; i < log; i++)
            {
                yield return n = (Int32)((10 * (n % divisor)) + Math.Floor(n / divisor));
            }
        }
    }
}
